library(ggplot2)
library(stargazer)
library(stringr)
library(stargazer)
source("functions.R")

d <- read.csv("surveydata_clean_study1.csv")

##data loading and cleaning

#delete lines with missing wave 2
d <- d[!is.na(d$disc_treatment.7),]

#leave out non-western respondents
d <- d[!d$nonwest,]

#new variables for evidenced (manifest) treatment, perceived (ambiguous) treatment,
#and any treatment at all
d$evidenced <- d$disc_treatment.7 %in% c("evidenced_stat", "evidenced_epis")
d$perceived <- d$disc_treatment.7 %in% c("perceived_stat", "perceived_epis")
d$treated <- d$disc_treatment.7 != "control"

#ordering of treatment group levels and their labels for figures
treatment_lvls <- c("evidenced_stat", "evidenced_epis", "perceived_stat", "perceived_epis")
treatment_labels <- c("audit\nstudy", "supermarket\nstory", "immigrant\npoll", "everyday\nracism")
d$disc_treatment.7 <- factor(d$disc_treatment.7, levels=c("control", treatment_lvls))

#imputing immigration attitudes
d$immiNA <- is.na(d$immi)
d$immi_impute <- d$immi #version with mean imputed for missing values
d$immi_impute[d$immiNA] <- mean(d$immi, na.rm=T) #dummy for whether a value was imputed


##Main analyses

#Effect of evidenced (manifest) v. perceived (ambiguous) discrimination on concern
fit_mani_ambi <- lm(disc_problem.7 ~ evidenced + immi_impute + immiNA, data=d[d$treated,])
summary(fit_mani_ambi)

#Effect of each treatment on concern (discrimination as a problem)
fit_concern <- lm(disc_problem.7 ~ disc_treatment.7 + immi_impute + immiNA, data=d)
summary(fit_concern)
#effect in SDs
fit_concern$coefficients / sd(d$disc_problem.7, na.rm=T) 

#Effects of each treatment on seeing discrimination as widespread
fit_widespr <- lm(disc_widespr.7 ~ disc_treatment.7 + immi_impute + immiNA, data=d)
summary(fit_widespr)
#effect in SDs
fit_widespr$coefficients / sd(d$disc_widespr.7, na.rm=T)

#figure 1: all treatments
plot_estCIs_S1(fit_concern, "discrimination_effects", "seeing discrimination as problem") #graph
plot_estCIs_S1(fit_widespr, "disc_widespr_effects", "believing discrimination widespread")  #graph

#Figure 2, left hand panels: evidenced (manifest) treatments only
plot_estCIs(fit_concern, "evidenced_only/discr_evidenced_effects_1", "seeing discrimination as problem",
            "disc_treatment.7", c("evidenced_stat", "evidenced_epis"),  treatment_labels=c("audit\nstudy", "supermarket\nstory"),
            title="Study 1", ymax=.51, w=3.5)
plot_estCIs(fit_widespr, "evidenced_only/discr_evidenced_effects_widespr_1", "believing discrimination widespread",
            "disc_treatment.7", c("evidenced_stat", "evidenced_epis"),  treatment_labels=c("audit\nstudy", "supermarket\nstory"),
            title="Study 1", ymax=1, w=3.5)


##appendix A: representativeness

#see separate script representativeness.R


##appendix D: descriptives


##appendix E: full results behind figures

#Effect of each treatment on concern and widespread
stargazer(list(fit_concern, fit_widespr),
          covariate.labels=str_to_title(c(treatment_labels,"Immigration Att.", "Imm. Att. Missing")),
          dep.var.labels=c("Problem","Widespread"), out="tables/fullfig12.tex",
          label="tab:fullfig12",
          omit.stat=c("f","ser"), digits=2,
          title="Effect of all treatments in Study 1 on seeing discrimination as a problem (1-5 scale)
          and as widespread (0-10 scale).")

#for pooled result, see separate script discrimination_analysis_pooled.R

##appendix F: full results behind figures without imputation

#Effect of each treatment on concern and widespread using immigration
#covariate without imputation (dropping NAs)
fit_concern_noimp <- lm(disc_problem.7 ~ disc_treatment.7 + immi, data=d)
fit_widespr_noimp <- lm(disc_widespr.7 ~ disc_treatment.7 + immi, data=d)
stargazer(list(fit_concern_noimp, fit_widespr_noimp),
          covariate.labels=str_to_title(c(treatment_labels,"Immigration Att.")),
          dep.var.labels=c("Problem","Widespread"), out="tables/fullfig12_noimp.tex",
          label="tab:fullfig12_noimp",
          omit.stat=c("f","ser"), digits=2,
          title="Effect of all treatments in Study 1 on seeing discrimination as a problem (1-5 scale)
          and as widespread (0-10 scale), without imputing missing immigration attitudes.")


##appendix G: weighted analyses

#Effect of each treatment on concern and widespread using wave 7 survey weights
fit_weight_concern <- lm(disc_problem.7 ~ disc_treatment.7 + immi_impute + immiNA, data=d, weights=Weight.7)
fit_weight_widespr <- lm(disc_widespr.7 ~ disc_treatment.7 + immi_impute + immiNA, data=d, weights=Weight.7)
stargazer(list(fit_weight_concern, fit_weight_widespr),
          covariate.labels=str_to_title(c(treatment_labels,"Immigration Att.", "Imm. Att. Missing")),
          dep.var.labels=c("Problem","Widespread"), out="tables/weighted_study1.tex",
          omit.stat=c("f","ser"), digits=2,
          title="Effect of all treatments in Study 1 on seeing discrimination as a problem (1--5 scale) and
          as widespread (1--10 scale), weighting respondents by survey weights.")


##appendix H: heterogeneous treatment effects

#for analyses pooling Study 1 and Study see separate script discrimination_analysis_pooled.R